package com.fg.fgmall.service.impl;

import com.fg.fgmall.entity.HomeHot;
import com.fg.fgmall.entity.HomeNew;
import com.fg.fgmall.entity.Product;
import com.fg.fgmall.mapper.HomeHotMappper;
import com.fg.fgmall.mapper.HomeNewMappper;
import com.fg.fgmall.mapper.ProductMapper;
import com.fg.fgmall.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页业务层实现
 */
@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    private HomeNewMappper homeNewMappper;

    @Autowired
    private HomeHotMappper homeHotMappper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<Product> selectNewProducts() throws Exception{

        List<Product> products = new ArrayList<>();
        List<HomeNew> homeNews = homeNewMappper.selectAll();
        for (HomeNew homeNew : homeNews) {
            Product product = productMapper.selectById(homeNew.getProductId());
            products.add(product);
        }

        return products;
    }

    @Override
    public List<Product> selectHotProducts() throws Exception {
        List<Product> products = new ArrayList<>();
        List<HomeHot> homeHots = homeHotMappper.selectAll();
        for (HomeHot homeHot : homeHots) {
            Product product = productMapper.selectById(homeHot.getProductId());
            products.add(product);
        }
        return products;
    }
}
